package com.ltu.config.wx.payment.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.ltu.util.SpringUtil;
import com.ltu.util.common.ProjectPath;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.RSAConfig;
import com.wechat.pay.java.core.cipher.Verifier;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.util.PemUtil;
import com.wechat.pay.java.service.merchantexclusivecoupon.MerchantExclusiveCouponService;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;

import lombok.AllArgsConstructor;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

/**
 * 20230803
 * @author ruochen
 *
 */
@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WxPayProperties.class)
@AllArgsConstructor
public class WxPayConfiguration {
	private WxPayProperties properties;
//	@Bean
//	@ConditionalOnMissingBean
	public WxPayService wxPayService() {
		WxPayConfig payConfig = new WxPayConfig();
		payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
		payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
		payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
		payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
		payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
		payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
		payConfig.setNotifyUrl(StringUtils.trimToNull(this.properties.getNotifyUrl()));
		payConfig.setCertSerialNo(StringUtils.trimToNull(this.properties.getCertSerialNo()));

		// 可以指定是否使用沙箱环境
		payConfig.setUseSandboxEnv(false);
		
		WxPayService wxPayService = new WxPayServiceImpl();
		wxPayService.setConfig(payConfig);
		return wxPayService;
	}
	
	

	/**
	 * @Description: 加载微信服务商证书
	 * @author 李生
	 * @date 2024-03-19 08:55:17 
	 * @return
	 */
	@Bean(name = "wxFwsRSAConfig") 
	public RSAAutoCertificateConfig wxFwsRSAConfig() {
		
//		String ruserDir = this.getClass().getClassLoader().getResource("").getPath();
//		String certPemPath = rootPath.concat(File.separator).concat("apiclient_cert.pem");
		
		String rootPath = System.getProperties().getProperty("user.dir").concat(File.separator).concat("cert");
		String keyPemPath = rootPath.concat(File.separator).concat("apiclient_key.pem");
		RSAAutoCertificateConfig config = new RSAAutoCertificateConfig.Builder()
								.merchantId(this.properties.getMchId())
								.privateKeyFromPath(keyPemPath)
								.merchantSerialNumber(this.properties.getCertSerialNo())
								.apiV3Key(this.properties.getMchKey())
								.build();
		return config;
	}

	
	
	/**
	 * @Description: 用来解解密、验签信商户平台的回调
	 * @author 李生
	 * @date 2024-03-19 09:28:18 
	 * @param config
	 * @return
	 */
//	@Bean(name = "wxNotificationParser")
//	public NotificationParser wxNotificationParser(RSAAutoCertificateConfig config ) {
//		NotificationParser parser = new NotificationParser(config);
//		return parser;
//	}
//	
	/**
	 * @Description: 商家券操作对象
	 * @author 李生
	 * @date 2024-03-19 08:55:17 
	 * @return
	 */
//	@Bean(name = "wxMchCouponService")
//	@ConditionalOnMissingBean
//	public MerchantExclusiveCouponService wxMchCouponService(RSAAutoCertificateConfig config) {
//		MerchantExclusiveCouponService	service = new MerchantExclusiveCouponService.Builder().config(config).build();
//		return service;
//	}
	
	
	/**
	 * @Description: 微信商家转账服务
	 * @author 李生
	 * @date 2024-03-19 03:56:33 
	 * @return
	 */
//	@Bean
//	@ConditionalOnMissingBean
	public TransferBatchService transferBatchService() {
//		String rootPath = ProjectPath.classPath;
		String rootPath = System.getProperties().getProperty("user.dir").concat(File.separator).concat("cert");
//		String certPemPath = rootPath.concat(File.separator).concat("apiclient_cert.pem");
		String keyPemPath = rootPath.concat(File.separator).concat("apiclient_key.pem");
		TransferBatchService service = null;

		RSAAutoCertificateConfig config = new RSAAutoCertificateConfig.Builder()
								.merchantId(this.properties.getMchId())
								.privateKeyFromPath(keyPemPath)
								.merchantSerialNumber(this.properties.getCertSerialNo())
								.apiV3Key(this.properties.getMchKey())
								.build();
		service = new TransferBatchService.Builder().config(config).build();

		return service;
	}
	

}
